package websimulator;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.filechooser.FileSystemView;
import javax.xml.ws.Response;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.omg.CORBA.Environment;

import com.sun.jmx.snmp.Timestamp;


/**
 * Servlet implementation class SimpleSimulator
 * Servlet "flushes" generated graph into the page
 */


@WebServlet(description = "Our Simulator servlet", urlPatterns = { "/SimpleSimulator" })


public class SimpleSimulator extends HttpServlet {
	
	Temperatur temp = new Temperatur();
	LevelOfIllumination levelOfIllumination = new LevelOfIllumination (true);
	
	
	private static final long serialVersionUID = 1L;
	private HttpSession httpsession; 
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		processRequest(request, response);
	}


	/**
	 * @param request, response
	 * @return simulated chart as a PNG.
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		processRequest(request, response);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		httpsession = request.getSession();
		if (httpsession.getAttribute("username")==null){
			response.sendRedirect("Login.jsp");
			return;
		}
		/**
		 * Gets user input from form.
		 */
		int population = Integer.parseInt (request.getParameter ("population"));
		double area = Double.parseDouble (request.getParameter ("areal"));
		double wallHeight = Double.parseDouble (request.getParameter("wallheight"));
		int directive = Integer.parseInt(request.getParameter("byggeforeskrift"));
		int buildingsType = Integer.parseInt(request.getParameter ("buildingstype"));
		
		for (int i = 0; i < 24; i++)
		{
			temp.addTemperaturInside (i, request.getParameter ("t" + i));			
			temp.addTemperaturOutside (i, request.getParameter ("to" + i));
			levelOfIllumination.addLevel (i, request.getParameter ("i" + i));						
		}
		
		temp.addTemperaturInside (24, request.getParameter ("t" + 0));
		temp.addTemperaturOutside (24, request.getParameter ("to" + 0));
		levelOfIllumination.addLevel (24, request.getParameter ("i" + 0));
		
		OutputStream out = response.getOutputStream();
		Graph graph = new Graph(population, area, wallHeight, directive, temp, levelOfIllumination, buildingsType);
		JFreeChart chart = graph.getSimulationChart();
	
		ChartUtilities.writeChartAsPNG(out, chart, 800, 800);	
		
	}
}